import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router } from "@angular/router"; //路由引入

declare var $: any;
@Component({
  selector: 'sa-data-audit',
  templateUrl: './data-audit.component.html',
})
export class DataAuditComponent implements OnInit {
	auditingTime:string = '';
	tableData = []; //table数据
	selected = []; //row选择的数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword

	constructor(private jsonApiService: JsonApiService, private router: Router) {}

	ngOnInit() {
		System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
			()=>{System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{this.initDate();});}
		);
		this.load();
	}
	//刷新
    refresh(){
    	this.load();
    }
	initDate(){
		common.initDatePicker("#auditingTime",0)
		$("#auditingTime").val(common.getNowTime());//当前时间
	}
	//表格搜索
	table_search(word) {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		let reqdata = 'gridPager={"pageSize":' + this.pageSize + ',"nowPage":' + this.curPage + ',"parameters":{"searchKey":"' + this.searchKey + '"}}'
		this.jsonApiService.http("system/datarewiew.html", reqdata).subscribe(data => {
			if(data&&data.exhibitDatas){
				for(let item of data.exhibitDatas){
					item.spaceTime = common.secondToDate(item.spaceTime);
				}
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//全部更新,启用,取消事件
	operationAll(type){
		var texts = "";
		if(type === 'updata'){
			texts = "全部更新";
		}else if(type === 'start'){
			texts = "全部启用";
		}else if(type === 'stop'){
			texts = "全部取消";
		}
		this.auditingTime = $("#auditingTime").val();
		var isDate=common.checkDateFormat(this.auditingTime);
		if(!isDate){
			this.jsonApiService.message('时间格式输入不正确...');
			return;
		}
		this.jsonApiService.confirm_message("确定要"+texts+"数据的审核?").then(()=>{
			let reqdata = 'deviceId=All&reviewDate='+this.auditingTime+'&isReview=true&status='+type;
			if(type=='stop'){
				  reqdata = 'deviceId=All&reviewDate='+this.auditingTime+'&isReview=false&status='+type;
			}
			this.jsonApiService.http("system/updatedatareview.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
	//选中单个更新事件
	updateStatus(data,type){
		var texts = "";
		if(type === 'updata'){
			texts = "更新";
		}else if(type === 'start'){
			texts = "启用";
		}else if(type === 'stop'){
			texts = "取消";
		}
		this.auditingTime = $("#auditingTime").val();
		this.jsonApiService.confirm_message("确定要"+texts+"数据的审核?").then(()=>{
			var isReview = false;
			if(type === 'update'||type === 'start'){
				isReview = true;
			}else if(type === 'stop'){
				isReview = false;
			}
			let reqdata = 'deviceId='+data.deviceId+'&reviewDate='+this.auditingTime+'&isReview='+isReview+'&status='+type;
			this.jsonApiService.http("system/updatedatareview.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}

	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除
	del_audit(data) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id=' + data.id;
			this.jsonApiService.http("system/deletedatareview.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
}
